export const debounced = (
  func: Function,
  delay: number,
  immediate: boolean = false
): Function => {
  let timer: number | undefined;
  return (...args: any) => {
    if (immediate) {
      func.apply(this, args); // 确保引用函数的指向正确，并且函数的参数也不变
      immediate = false;
      return;
    }
    clearTimeout(timer);
    timer = setTimeout(() => {
      func.apply(this, args);
    }, delay) as unknown as number;
  };
};

export const throttle = (fn: Function, time = 300) => {
  let canRun: boolean = true;
  return function () {
    if (!canRun) return;
    canRun = false;
    setTimeout(() => {
      fn();
      canRun = true;
    }, time);
  };
};
